<?
/************code by seven****************/
require("../../common/init.php");
require("author.php");
require("upfile.php");

$gee=$tags; // gee, 意为赶车时候喊的"驾", 此处用来表示次页面的主角对象
$gee_tag_list = $tag_list;

$url='tags_manage';
$s=isset($_GET["s"])?clean($_GET["s"]):''; // search
$do=isset($_GET["do"])?clean($_GET["do"]):'view'; // 操作: 查看/编辑/删除
$page=1; if(isset($_GET["page"]))if(is_numeric($_GET["page"]))$page=$_GET["page"];
$action=isset($_POST["action"])?clean($_POST["action"]):''; // 数据库操作: CURD
$id=0; if (isset($_GET["id"])) $id = clean($_GET["id"]);
$filter="0";
if(isset($_GET["filter"]))$filter=clean($_GET["filter"]);
if(isset($_POST["filter"]))$filter=clean($_POST["filter"]);
$tags_title = '';
switch ($filter){
    case 0:
        $title=ROOT_TAGS;
        break;
    case 1:
        $title=ROOT_TAGS;
        break;
    case 2:
        $title=ROOT_TAGS;
        break;
    default:
        $title = $tags_title;
}
//$title = $tags_title;

// sql action / sql 动作
switch ($action){
    case "update":
        sql_update();
        break;
    case "add":
        sql_insert();
        break;
}
if($id==0){
    $order='id';
    $where=array();
    if($s!='') {
        $where[]=array('name'=>'name_'.$lang,'val'=>$s,'opn'=>'like', 'and'=>'and');
    }
//    print_r($where);
    $pages=$gee->count_pages($where);
    $info=$gee->get_page($page,$where,$order);
}
else $info=$gee->get_row($id);
//pre($info);

function add()
{
    global $h, $url, $type;
    $row = array(
    );
    echo $h->form($url.'.php', 1, '');
    show_box($row, true, true);
    echo $h->hidden('action', 'add');
    echo $h->former();
}
function update($info)
{
    global $gee, $h, $url, $id, $type;
    if($id=="")
    {
        echo "Invali argument!!!";
        exit;
    }
    echo $h->form($url.'.php', 1, '');
    show_box($info, true, false);
    echo $h->hidden('action', 'update');
    echo $h->former();
}
function delete($id)
{
    global $gee,$url, $type, $gee_tag_list;
    $info=$gee->get_row($id);
    if($gee->delete($id)){
        $gee_tag_list->delete($id, 'tag_id');
        wheel("$url.php");
    }
}
function show_info($info)
{
    global $id;
    if($id==0)
    {
        show_boxes($info);
    }
    else
    {
        show_box($info, false);
    }
}
function sql_insert()
{
    global $gee,$url,$lang, $type;
    $tags_name = $_POST['name'];
    $gee_empty = $gee->get_row($tags_name, 'name_'.$lang);
    if(!$gee_empty){
        $arr=array(
            'name_'.$lang=>$_POST["name"],
            'is_filter'=>$_POST["is_filter"],
            'is_index'=>$_POST["is_index"],
            'count' => 0,
            'user_id'=>decrypt($_COOKIE['userid']),
        );
    //pre($arr); exit;
        if($gee->insert($arr))
            yell(ROOT_ADD_SUCCESS,$url.'.php');
    }else{
        yell(ROOT_ADD_SUCCESS,$url.'.php');
    }
}
function sql_update()
{
    global $gee, $url,$lang, $type;
    $candelete=0;
    $id=$_POST["id"];
    $tags_name = $_POST['name'];
    $gee_empty = $gee->get_row($tags_name, 'name_'.$lang);
    if(!$gee_empty){
        $arr=array(
            'name_'.$lang=>$_POST["name"],
            'is_filter'=>$_POST["is_filter"],
            'is_index'=>$_POST["is_index"],
            'user_id'=>decrypt($_COOKIE['userid']),
        );
        //pre($arr); exit;
        if($gee->update($arr, 'id', $id ) ) {
            yell(ROOT_UPDATE_SUCCESS,$url.'.php');
        }else{
            yell(ROOT_UPDATE_SUCCESS,$url.'.php');
        }
    }
}

// view 加载视图
require ROOTVIEW.$url.'.php';

// end
